﻿using PagedList;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace VehicleOOBWeb.Models
{
    /// <summary>
    /// Lớp thao tác dữ liệu trên các đơn hàng
    /// </summary>
    public class OrderService
    {
        /// <summary>
        /// Thêm mới đơn hàng
        /// </summary>
        /// <param name="order"></param>
        /// <returns></returns>
        public static bool AddOrder(Order order)
        {
            using (DataVehicleDataContext db = new DataVehicleDataContext())
            {
                db.Orders.InsertOnSubmit(order);
                if (order.OrderDetails.Count > 0)
                {
                    db.OrderDetails.InsertAllOnSubmit(order.OrderDetails);
                }
                db.SubmitChanges();
                return true;
            }
        }

        /// <summary>
        /// Cập nhật đơn hàng
        /// </summary>
        /// <param name="order"></param>
        /// <returns></returns>
        public static bool UpdateOrder(Order order)
        {
            using (DataVehicleDataContext db = new DataVehicleDataContext())
            {
                Order update_order = db.Orders.FirstOrDefault(o => o.order_id == order.order_id);
                if (update_order != null)
                {
                    update_order.actual_shipped_date = order.actual_shipped_date;
                    update_order.received_date = order.received_date;
                    update_order.shipped_date = order.shipped_date;
                    update_order.username = order.username;
                    update_order.OrderDetails = order.OrderDetails;
                    db.SubmitChanges();
                }
                return true;
            }
        }

        /// <summary>
        /// Xóa đơn hàng
        /// </summary>
        /// <param name="order_id"></param>
        /// <returns></returns>
        public static bool DeleteOrder(int order_id)
        {
            using (DataVehicleDataContext db = new DataVehicleDataContext())
            {
                Order order = db.Orders.FirstOrDefault(o => o.order_id == order_id);
                if (order != null)
                {
                    db.OrderDetails.DeleteAllOnSubmit(order.OrderDetails);
                    db.Orders.DeleteOnSubmit(order);
                    db.SubmitChanges();
                }
                return true;
            }
        }

        /// <summary>
        /// Lấy thông tin đơn hàng
        /// </summary>
        /// <param name="order_id"></param>
        public static Order GetOrderByID(int order_id)
        {
            using (DataVehicleDataContext db = new DataVehicleDataContext())
            {
                Order order = db.Orders.FirstOrDefault(o => o.order_id == order_id);
                if (order != null)
                {
                    var orderDetails = order.OrderDetails;
                    return order;
                }
                else
                {
                    return null;
                }
            }
        }

        /// <summary>
        /// Lấy danh sách đơn hàng theo người mua
        /// </summary>
        /// <param name="order_id"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        public static IPagedList<Order> GetListOrderByUser(int? order_id, int pageIndex, int pageSize)
        {
            using (DataVehicleDataContext db = new DataVehicleDataContext())
            {
                IEnumerable<Order> lstOrder = null;
                if (order_id.HasValue)
                {
                    lstOrder = (from o in db.Orders
                                where o.order_id.ToString().Contains(order_id.Value.ToString())
                                select o);
                }
                else
                {
                    lstOrder = db.Orders;
                }

                if (lstOrder != null)
                {
                    return lstOrder.ToPagedList(pageIndex, pageSize);
                }
                else
                {
                    return null;
                }
            }
        }

        /// <summary>
        /// Lấy danh sách đơn hàng có tìm kiếm theo người mua và mã đơn hàng
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="order_id"></param>
        /// <param name="order_user"></param>
        public static IPagedList<Order> GetListOrder(int? order_id, string order_user, int pageIndex, int pageSize)
        {
            using (DataVehicleDataContext db = new DataVehicleDataContext())
            {
                IEnumerable<Order> lstOrder = null;
                if (order_id.HasValue && !string.IsNullOrEmpty(order_user))
                {
                    order_user = order_user.Trim().ToLower();
                    lstOrder = (from o in db.Orders
                                where o.order_id.ToString().Contains(order_id.Value.ToString())
                                    || o.username.ToLower().Contains(order_user)
                                select o);
                }
                else
                    if (order_id.HasValue)
                    {
                        lstOrder = (from o in db.Orders
                                    where o.order_id.ToString().Contains(order_id.Value.ToString())
                                    select o);
                    }
                    else
                        if (!string.IsNullOrEmpty(order_user))
                        {
                            lstOrder = (from o in db.Orders
                                        where o.username.ToLower().Contains(order_user)
                                        select o);
                        }
                        else
                        {
                            lstOrder = db.Orders;
                        }

                if (lstOrder != null)
                {
                    return lstOrder.ToPagedList(pageIndex, pageSize);
                }
                else
                {
                    return null;
                }
            }
        }
    }


    public class Cart
    {
        public string item_id { get; set; }

        public int item_quantity { get; set; }

        public string item_name { get; set; }

        public string item_price { get; set; }

        public string item_manufacturer { get; set; }
    }
}